home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / TOOLPAS2 / AFILES.PAS < prev    next >
Pascal/Delphi Source File  |  1988-11-29  |  4KB  |  191 lines

  1.  
  2. (*
  3.  * afiles - find all files in a subdirectory tree
  4.  *
  5.  * Author: S.H.Smith, 5-Apr-86
  6.  *
  7.  *)
  8.  
  9. uses DOS;
  10.  
  11. {$r+,s+}
  12.  
  13. {$I treelib.inc  Utility to create and format visual trees}
  14.  
  15. const
  16.    version = 'afiles v2.0  (S.H.Smith, 11-29-88)';
  17.  
  18. type
  19.    anystring = string[65];
  20.  
  21. var
  22.    allfiles:  boolean;
  23.  
  24.  
  25. (*
  26.  * get_sub_directories - recursively get all subdirectories for a given
  27.  *                       path and put them into a given node record
  28.  *
  29.  *)
  30.  
  31. procedure get_sub_directories (path:       anystring;
  32.                                var node:   node_ptr);
  33. var
  34.    reg:      registers;
  35.    dta:      string[255];
  36.    pattern:  anystring;
  37.    filename: anystring;
  38.    cf:       byte;
  39.    c:        char;
  40.    i:        integer;
  41.    isadir:   array [subnode_index] of boolean;
  42.    dirbit:   boolean;
  43.  
  44. begin
  45.    reg.ax := $1a00;
  46.    reg.ds := seg (dta [1]);
  47.    reg.dx := ofs (dta [1]);
  48.    msdos(reg);              {set dta address}
  49.  
  50.    pattern := path + '*.*' + #0;
  51.    reg.ax := $4e00;
  52.    reg.cx := $37;           {match any file attribute but volume label}
  53.    reg.ds := seg (pattern [1]);
  54.    reg.dx := ofs (pattern [1]);
  55.    msdos(reg);              {find first subdirectory}
  56.  
  57.    cf := reg.flags and 1;
  58.  
  59.    while (cf <> 1) and (node^.count < maxsubs) do
  60.    begin
  61.       filename := '';
  62.       i := 0;
  63.  
  64.       repeat
  65.          c := dta [31 + i];        {build up filename from dta buffer}
  66.          if c <> #0 then
  67.             filename := filename + c;
  68.  
  69.          i := i + 1;
  70.       until c = #0;
  71.  
  72.       if filename[1] = '.' then
  73.          filename := '';            {exclude . and .. from listing}
  74.  
  75.       dirbit := (ord(dta [22]) and $10) <> 0;
  76.  
  77.       if filename <> '' then
  78.       with node^ do                 {add filename to the node, if ok}
  79.       begin
  80.          if count = 0 then
  81.             new(subs);
  82.  
  83.          count := count + 1;
  84.          subs^[count] := new_node;
  85.          isadir[count] := dirbit;
  86.  
  87.          if dirbit then
  88.             subs^[count]^.name := filename + '\'
  89.          else
  90.             subs^[count]^.name := filename;
  91.       end;
  92.  
  93.       reg.ax := $4f00;
  94.       reg.ds := seg (dta [1]);
  95.       reg.dx := ofs (dta [1]);
  96.       msdos(reg);              {keep searching for next file}
  97.  
  98.       cf := reg.flags and 1;
  99.    end;
  100.  
  101.    if cf = 0 then
  102.       writeln('warning:  more than ',maxsubs,' entries in ',path);
  103.  
  104.  
  105.    with node^ do        {find the subdirectories for each of the
  106.                          directories found in this node}
  107.  
  108.       for i := 1 to count do
  109.          if isadir[i] then
  110.             get_sub_directories(path + subs^[i]^.name, subs^[i])
  111.          else
  112.             writeln(path + subs^[i]^.name);
  113.  
  114.    sort_node(node);
  115.  
  116. end;                     {subdirs}
  117.  
  118.  
  119.  
  120. (*
  121.  * dotree - build and output the tree for a given path
  122.  *
  123.  *)
  124.  
  125. procedure dotree(path:  anystring);
  126. var
  127.    root:  node_ptr;
  128.    i:     integer;
  129.  
  130. begin
  131.  
  132. (* make the path all upper case.  make sure it always ends with '\' *)
  133.  
  134.    for i := 1 to length(path) do
  135.       path[i] := upcase(path[i]);
  136.  
  137.    if path[length(path)] <> '\' then
  138.       path := path + '\';
  139.  
  140.  
  141. (* create a new (empty) tree structure *)
  142.  
  143.    root := new_node;
  144.    root^.name := path;
  145.  
  146.  
  147. (* scan all subdirectories and build them into the tree *)
  148.  
  149.    get_sub_directories(path, root);
  150.  
  151. end;
  152.  
  153.  
  154.  
  155. (*
  156.  * print program usage instructions
  157.  *
  158.  *)
  159.  
  160. procedure usage;
  161. begin
  162.    writeln;
  163.    writeln(version);
  164.    writeln;
  165.    writeln('Usage:    afiles DIRECTORY_PATH >FILE_LIST');
  166.    writeln;
  167.    writeln('Example:  afiles \shs\proj | tar c a:backup -f');
  168.    halt;
  169. end;
  170.  
  171.  
  172.  
  173. (*
  174.  * main program
  175.  *
  176.  *)
  177.  
  178. var
  179.    i: integer;
  180.  
  181. begin
  182.    if paramcount < 1 then
  183.       usage;
  184.  
  185. (* process the specified paths *)
  186.  
  187.    for i := 1 to paramcount do
  188.       dotree(paramstr(i));
  189. end.
  190.  
  191.